﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace ajayumi.Platform.Win.DataGridView
{
    /// <summary>
    /// DataGridViewEx.cs
    /// Author   : 黄乙冬
    /// Date     : 2011-10-27
    /// </summary>
    [ToolboxItem(true)]
    public class DataGridViewEx : System.Windows.Forms.DataGridView
    {
        private Color m_RowsDefaultBackColor = Color.FromArgb(255, 255, 255);
        [Description("行单元格背景色")]
        [Category("Extend Property")]
        public Color RowsDefaultBackColor
        {
            get
            {
                return m_RowsDefaultBackColor;
            }
            set
            {
                m_RowsDefaultBackColor = value;
            }
        }

        private Color m_AlternatingRowsDefaultBackColor = Color.FromArgb(169, 227, 255);
        [Description("间隔行单元格背景色")]
        [Category("Extend Property")]
        public Color AlternatingRowsDefaultBackColor
        {
            get
            {
                return m_AlternatingRowsDefaultBackColor;
            }
            set
            {
                this.m_AlternatingRowsDefaultBackColor = value;
            }
        }

        public DataGridViewEx()
            : base()
        {
            this.ColumnHeadersDefaultCellStyle = new DataGridViewCellStyle()
            {
                Alignment = DataGridViewContentAlignment.MiddleCenter,
                Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)))
            };

            this.CellClick += (sender, e) =>
            {
                if (e.ColumnIndex == 0 && e.RowIndex == -1) //如果单击列表头，全选．
                {
                    if (this.Columns[0] is DataGridViewCheckBoxColumn)
                    {
                        int i;
                        for (i = 0; i < this.RowCount; i++)
                        {
                            this.EndEdit();//结束编辑状态．
                            this.Rows[i].Cells[0].Value = "true";   //如果为true则为选中,false未选中
                            //if (this.MultiSelect)
                            //{
                            //    this.Rows[i].Selected = true;   //选中整行。
                            //}
                        }
                    }
                }
            };
        }

        protected override void OnRowPostPaint(System.Windows.Forms.DataGridViewRowPostPaintEventArgs e)
        {
            base.OnRowPostPaint(e);

            // 这是加入记录号的代码
            using (SolidBrush b = new SolidBrush(this.RowHeadersDefaultCellStyle.ForeColor))
            {
                e.Graphics.DrawString((Convert.ToInt32(e.RowIndex) + 1).ToString(System.Globalization.CultureInfo.CurrentCulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
            }

            //这是了隔行换色的代码
            this.RowsDefaultCellStyle.BackColor = this.RowsDefaultBackColor;
            this.AlternatingRowsDefaultCellStyle.BackColor = this.AlternatingRowsDefaultBackColor;
        }



        /// <summary>
        /// checkbox的单元格改变事件
        /// </summary>
        /// <param name="columnIndex"></param>
        /// <param name="rowIndex"></param>
        internal void OnCheckBoxCellCheckedChange(int columnIndex, int rowIndex, bool value)
        {
            bool existsChecked = false, existsNoChecked = false;
            DataGridViewCheckBoxCellEx cellEx;
            foreach (DataGridViewRow row in this.Rows)
            {
                cellEx = row.Cells[columnIndex] as DataGridViewCheckBoxCellEx;
                if (cellEx == null) return;
                existsChecked |= cellEx.Checked;
                existsNoChecked |= !cellEx.Checked;
            }

            DataGridViewCheckBoxColumnHeaderCellEx headerCellEx =
                this.Columns[columnIndex].HeaderCell as DataGridViewCheckBoxColumnHeaderCellEx;

            if (headerCellEx == null) return;

            CheckState oldState = headerCellEx.CheckedAllState;

            if (existsChecked)
                headerCellEx.CheckedAllState = existsNoChecked ? CheckState.Indeterminate : CheckState.Checked;
            else
                headerCellEx.CheckedAllState = CheckState.Unchecked;

            if (oldState != headerCellEx.CheckedAllState)
                this.InvalidateColumn(columnIndex);
        }

        /// <summary>
        /// 全选中/取消全选中
        /// </summary>
        /// <param name="columnIndex"></param>
        /// <param name="isCheckedAll"></param>
        internal void OnCheckAllCheckedChange(int columnIndex, bool isCheckedAll)
        {
            DataGridViewCheckBoxCellEx cellEx;
            foreach (DataGridViewRow row in this.Rows)
            {
                cellEx = row.Cells[columnIndex] as DataGridViewCheckBoxCellEx;
                if (cellEx == null) continue;
                cellEx.Checked = isCheckedAll;
            }
        }
    }
}
